home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / utility / amiga2date.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  3KB  |  119 lines

  1. /*
  2.     $Id: amiga2date.c,v 1.1 1996/08/31 12:58:11 aros Exp $
  3.     $Log: amiga2date.c,v $
  4.     Revision 1.1  1996/08/31 12:58:11  aros
  5.     Merged in/modified for FreeBSD.
  6.  
  7.     Desc:
  8.     Lang: english
  9. */
  10. #include "utility_intern.h"
  11.  
  12. /*****************************************************************************
  13.  
  14.     NAME */
  15.         #include <clib/utility_protos.h>
  16.  
  17.         __AROS_LH2(void, Amiga2Date,
  18.  
  19. /*  SYNOPSIS */
  20.         __AROS_LHA(unsigned long     , seconds, D0),
  21.         __AROS_LHA(struct ClockData *, result, A0),
  22.  
  23. /*  LOCATION */
  24.         struct Library *, UtilityBase, 20, Utility)
  25.  
  26. /*  FUNCTION
  27.         Convert the time value given as the number of seconds since the
  28.         1st of January 1978 (00:00:00 1.1.78), to a more useful values,
  29.         which is easier for most people to understand. These values will
  30.         be stored in the ClockData structure whose address is passed as
  31.         an argument.
  32.  
  33.     INPUTS
  34.         seconds     -   Number of seconds since 1.1.78 00:00:00
  35.         result      -   The ClockData structure to store the information
  36.                         in.
  37.  
  38.     RESULT
  39.         The ClockData structure will contain the converted time values.
  40.  
  41.     NOTES
  42.  
  43.     EXAMPLE
  44.  
  45.     BUGS
  46.  
  47.     SEE ALSO
  48.  
  49.     INTERNALS
  50.         Some information about some constants I use:
  51.  
  52.              731 =  365 + 366, the number of days between 1.1.1978 and
  53.                     1.1.1976. Using 1976 makes working out leap years
  54.                     simpler.
  55.             1461 =  The number of days in four years including 1 leap year.
  56.                     (eg 365*3 + 366)
  57.            86400 =  The number of seconds in one day.
  58.  
  59.         I used these as constants so that they don't have to be computed
  60.         on the fly, or read from variables.
  61.  
  62.     HISTORY
  63.         29-10-95    digulla automatically created from
  64.                             utility_lib.fd and clib/utility_protos.h
  65.         19-05-96    iaint   Wrote, with a little help from a Perl package.
  66.         11-08-96    iaint   Updated for the new AROS format.
  67.         17-08-96    iaint   Removed calls to unimplemented UDivMod32/UMult32
  68.  
  69. *****************************************************************************/
  70. {
  71.     __AROS_FUNC_INIT
  72.  
  73.     static const ULONG dim[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  74.     ULONG days, temp, leap;
  75.  
  76.     days = seconds / 86400;
  77.     result->wday = days % 7;
  78.  
  79.     /*
  80.         using the number of days since 1.1.76 makes leap year calculations
  81.         simpler.
  82.     */
  83.  
  84.     days += 731;
  85.  
  86.     result->sec = seconds % 60;
  87.     seconds /= 60;
  88.     result->min = seconds % 60;
  89.     seconds /= 60;
  90.     result->hour= seconds % 24;
  91.  
  92.     /* Number of sets of four years since 1976 */
  93.     temp = days / 1461;
  94.  
  95.     /* days since the beginning of the last leap year  */
  96.     days %= 1461;
  97.  
  98.     temp = 1976 + (temp << 2);
  99.  
  100.     leap = (days <= 365);
  101.     if(!leap) /* not a leap year */
  102.     {
  103.         temp++;
  104.         days -= 366;
  105.         result->year = temp + (days / 365);
  106.         days %= 365;
  107.     }
  108.  
  109.     /* days now contains the days since the beginning of the current year */
  110.     for(temp = 0; (temp == 1) ? (days >= 28 + leap) : (days >= dim[temp]); temp++)
  111.         days -= (temp == 1) ? (28 + leap) : dim[temp];
  112.  
  113.     result->month = temp;
  114.     result->mday = days + 1;
  115.  
  116.     __AROS_FUNC_EXIT
  117.  
  118. } /* Amiga2Date */
  119.